const { ccclass, property } = cc._decorator;



// import {Rx} from '../trd/rxjs/rx.all'
import Http from '../common/util/Http'
// import {XmdyManager} from '../xmdy/XmdyManager'
var manager = require("XmdyManager")

@ccclass
export default class Login extends cc.Component {

    @property(cc.EditBox)
    username: cc.EditBox = null;
    @property(cc.EditBox)
    password: cc.EditBox = null;

    @property(cc.Label)
    info:cc.Label = null;
    http: Http = null;
    userPlugin = null;

    destory() {
        if (cc.sys.isMobile) {
            this.userPlugin.removeListener();
        }

    }


    onUserResult(code, msg) {
        cc.log("on user result action.");
        cc.log("msg:" + msg);
        cc.log("code:" + code);        //这里可以根据返回的 code 和 msg 做相应的处理


        switch (code) {
            case anysdk.UserActionResultCode.kLoginSuccess:
                //do something
                var msgJson = JSON.parse(msg)
                
                manager.token = msgJson.token
                this.http.getJson(Http.HALL_URL_PREFIX + "user/info", { token: msgJson.token })
                .subscribe(info=>{
                    this.toHall(info)
                 })


                break;
        }

    }

    
    toHall(data){
        manager.user = data;
        cc.director.loadScene('Hall')
    }

    login() {

        //登录获取用户id
        this.http.getJson(Http.LOGIN_URL_PREFIX + "login/account", { username: this.username.string, password: this.password.string })
        .flatMap((data)=>{
            manager.token = data.token; 
            return this.http.getJson(Http.HALL_URL_PREFIX + "user/info", { token: data.token })
        })
        .subscribe(info=>{
           this.toHall(info)
        })
//根据用户id刷新用户信息
       
        
    }

  
    loginWeixin() {
        this.userPlugin.login()
    }
    onLoad() {


        this.http = new Http();
        if (cc.sys.isMobile) {
            var agent = anysdk.agentManager;
            this.userPlugin = agent.getUserPlugin();
            this.userPlugin.setListener(this.onUserResult, this);
        } else {
            this.userPlugin = {
                login: function () {
                    cc.log("不是手机环境，调用微信失败")
                },logout: function () {
                    cc.log("不是手机环境，调用微信登出失败")
                    
                }
            }
        }


    }

}
